package com.hyg.lib_music.presenter;

import android.content.Context;

import com.hyg.lib_common.DataModel.Music.MusicCategoryData;
import com.hyg.lib_common.DataModel.Music.response.CategoryListResponse;
import com.hyg.lib_music.network.NetManager;
import com.hyg.lib_music.util.MusicFavManager;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Single;

public class MusicCategoryListPresenter implements MusicCategoryListContract.Presenter{

    private MusicCategoryListContract.View mFragment;
    private CloseableRxServiceExecutor executor;

    @Inject
    public MusicCategoryListPresenter(Context context,
                                      CloseableRxServiceExecutor executor) {
        this.executor = executor;
    }

    @Override
    public void setContractView(MusicCategoryListContract.View fragment) {
        mFragment = fragment;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {
    }

    /**
     * 分类列表
     *
     * @return
     */
    public Single<CategoryListResponse> categoryListRequest(int categoryId) {
        return Single.create(emitter -> emitter.onSuccess(NetManager.sharedManager().categoryListRequest(categoryId)));
    }

    /**
     * 获取子分类
     *
     * @return
     */
    @Override
    public void getCategoryList(int categoryId){
        executor.execute(
                this.categoryListRequest(categoryId),
                response -> {
                    if (response.code == 200)
                    {
                        List<MusicCategoryData> data= response.data;
                        MusicFavManager.sharedManager().syncMusicCategory(data);
                        mFragment.setCategoryListData(data);
                    }else {
                        //失败逻辑
                        mFragment.setCategoryListData(null);
                    }
                },
                error -> {
                    mFragment.setCategoryListData(null);
//                    Logger.d(error);
                });
    }

    @Override
    public void existSubCategoryList(int categoryId) {
        executor.execute(

                this.categoryListRequest(categoryId),
                response -> {
                    if (response.code == 200)
                    {
                        List<MusicCategoryData> data= response.data;
                        mFragment.enterSubCategory(data);
                    }else {
                        //失败逻辑
                    }
                },
                error -> {
//                    Logger.d(error);
                });
    }


}